package cn.dw.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.dw.domain.People;
import cn.dw.domain.Point;
import cn.dw.service.PeopleService;
import cn.dw.service.PointService;

public class PointAction extends ActionSupport implements ModelDriven<Point> {
	private static final long serialVersionUID = 1425408271648909369L;
	private Point point = new Point();
	private PointService pts;
	private PeopleService ps;
	private ChangetoJson ctj = new ChangetoJson();

	public String list() throws Exception {
		Map<String, Object> map = null;

		List<Point> points = this.pts.getAll();

		List<Map<String, Object>> list = new ArrayList<>();

		Point po = new Point();
		if (points.size() > 0) {
			for (int i = 0; i < points.size(); i++) {
				po = (Point) points.get(i);

				map = new HashMap<>();

				List<People> peoples = this.ps.getByPointId(po.getId());
				if (peoples.size() > 0) {
					for (int j = 0; j < peoples.size(); j++) {
						People people = (People) peoples.get(j);
						if (people.getLevel().intValue() == 1) {
							map.put("id", po.getId());
							map.put("name", po.getName());
							map.put("addr", po.getAddr());
							map.put("lng", po.getLng().toString());
							map.put("lat", po.getLat().toString());
							map.put("people", people.getName());
							map.put("phone", people.getPhone());

							list.add(map);
						}
					}
				}
			}
		}
		this.ctj.json(list);
		return null;
	}

	public void setPts(PointService pts) {
		this.pts = pts;
	}

	public Point getModel() {
		return this.point;
	}

	public void setPs(PeopleService ps) {
		this.ps = ps;
	}
}
